package com.marvels.product.param.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marvels.common.constants.MarvelsConstant;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.product.param.dao.McptLateFeeMapper;
import com.marvels.product.param.dto.McptLateFeeDTO;
import com.marvels.product.param.dto.McptProduct;
import com.marvels.product.param.service.LateFeeService;
@Service
public class LateFeeServiceImpl implements LateFeeService{
	@Autowired
	private McptLateFeeMapper mcptLateFeeMapper;

	@Override
	public PageInfo<McptLateFeeDTO> getPageInfoByParam(Map<String, Object> param) throws MarvelsBusinessException{
	Integer currentPage = param.get("currentPage")==null?1:(Integer)param.get("currentPage");
		
		Integer pageSize = param.get("pageSize")==null?20:(Integer)param.get("pageSize");
		
		PageHelper.startPage(currentPage, pageSize);
		
		List<McptLateFeeDTO>  mcptConsultingFeeList = mcptLateFeeMapper.getListByParam(param);
		
		PageInfo<McptLateFeeDTO> pageInfo = new PageInfo<McptLateFeeDTO>(mcptConsultingFeeList);
	    
		return pageInfo;
	}

	@Override
	public McptLateFeeDTO getEntityByPrimaryKey(Long lateFeeId) throws MarvelsBusinessException{
		// TODO Auto-generated method stub
		return mcptLateFeeMapper.selectByPrimaryKey(lateFeeId);
	}

	@Override
	public int insert(McptLateFeeDTO dto) throws MarvelsBusinessException{
		// TODO Auto-generated method stub
		return mcptLateFeeMapper.insert(dto);
	}

	@Override
	public int updateByPrimaryKey(McptLateFeeDTO dto) throws MarvelsBusinessException{
		// TODO Auto-generated method stub
		return mcptLateFeeMapper.updateByPrimaryKey(dto);
	}

	@Transactional(rollbackFor=Exception.class)
	public int updateStatus(McptLateFeeDTO dto) throws MarvelsBusinessException{
		Integer count = 0;
		try {
			 count  = mcptLateFeeMapper.updateStatus(assemblyParam(dto));
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>滞纳金审核异常",e);
			throw new MarvelsBusinessException(e);
		}
		return count;
	}

	@Override
	public McptLateFeeDTO getLateFeeByLateFeeCode(McptProduct dto) throws MarvelsBusinessException {
		return mcptLateFeeMapper.selectLateFeeByLateFeeCode(dto);
	}
	/**
	 * 组装参数
	 * @param record
	 * @return
	 * @throws Exception
	 */
	private McptLateFeeDTO assemblyParam(McptLateFeeDTO record) throws Exception{
		String userName = MarvelsConstant.SYSTEM_USER;
		 UserDTO user = CacheUtils.getCacheUser();
		 if(user!=null){
			 userName = user.getUserName();
		 }
		 record.setUpdatedBy(userName);
		 return record;
	}
	@Transactional(rollbackFor=Exception.class)
	public int delete(McptLateFeeDTO dto) throws MarvelsBusinessException {
		Integer count = 0;
		try {
			dto.setDeleteFlag(Byte.valueOf("1")); 
			count  = mcptLateFeeMapper.deleteByPrimaryKey(assemblyParam(dto));
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>滞纳金删除异常",e);
			throw new MarvelsBusinessException(e);
		}
		return count;
	}
	
}
